/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.addproduct;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.InventoryRecord;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.struts.support.BaseActionSupport;

import enumeration.InventoryOperation;

/**
 * @author dev.zhang
 * 
 */
public class View extends BaseActionSupport {

	private static final long serialVersionUID = 4793296582317707612L;
	
	private Integer id;
	private InventoryRecord record;
	@Autowired
	private SystemService systemService;
	@Autowired
	private InventoryService inventoryService;
	private String orderNumber;
	private String soNumber;
	private Integer currPageId;
	
	@Action("/warehouse/addproduct/view/addpurchare/${id}")
	public String addPurchare(){
		
		record = (InventoryRecord) systemService.get(InventoryRecord.class, id);
		
		return "add";
	}
	@Action("/warehouse/addproduct/view/doaddpurchare")
	public String doAddPurchare(){
		List<InventoryRecord> list = null;
		//String hqlSentence = "FROM InventoryRecord inv WHERE (inv.lastInventoryOperation = '"+InventoryOperation.OUT +"' OR inv.lastInventoryOperation = '"+InventoryOperation.TRANSFERRED+"')";
		String hqlSentence = "FROM InventoryRecord inv WHERE (inv.lastInventoryOperation = '"+InventoryOperation.OUT +"' OR inv.lastInventoryOperation = '"+InventoryOperation.TRANSFERRED+"')";
		//if (searchComponent.getOrderParameters().isEmpty()){
		//	searchComponent.addSearchElement("groupBy.inv.purchaseOrder.id","");
			//SElECT DISTINCT inv.purchaseOrder.id 
		//}
		//inventoryRecords = systemService.searchForList(hqlSentence, new HashMap());
		SearchComponent searchComponent = new SearchComponent();
		searchComponent.addSearchElement("eq.inv.purchaseOrder.id", id);
		list = systemService.searchForList(hqlSentence, searchComponent);
		//System.out.println(list);
		
		
		try {
			inventoryService.addProduct(list,soNumber);
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
			e.printStackTrace();
			logger.info(e.getStackTrace() +e.getMessage());
			return ERROR;
		}
		
		//setInfoMessage("1111");
		
		return "r:/warehouse/addproduct/index/"+currPageId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public InventoryRecord getRecord() {
		return record;
	}
	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	public String getSoNumber() {
		return soNumber;
	}
	public void setSoNumber(String soNumber) {
		this.soNumber = soNumber;
	}
	public Integer getCurrPageId() {
		return currPageId;
	}
	public void setCurrPageId(Integer currPageId) {
		this.currPageId = currPageId;
	}

	
	
}
